package com.blog.backend.service.impl;

import com.blog.backend.config.CodeMsg;
import com.blog.backend.domain.BlogAlbum;
import com.blog.backend.exception.GlobalException;
import com.blog.backend.mapper.BlogAlbumMapper;
import com.blog.backend.mapper.ImageMapper;
import com.blog.backend.req.AddAlbumReq;
import com.blog.backend.req.PageAlbumReq;
import com.blog.backend.resp.ArticleResp;
import com.blog.backend.resp.BlogAlbumResp;
import com.blog.backend.resp.PictureUrlResp;
import com.blog.backend.service.BlogAlbumService;
import com.blog.backend.utils.PageUtils;
import com.blog.backend.utils.UuidUtils;
import com.blog.backend.vo.PageRequest;
import com.blog.backend.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BlogAlbumServiceImpl implements BlogAlbumService {

    @Resource
    private BlogAlbumMapper blogAlbumMapper;

    @Resource
    private ImageMapper imageMapper;
    @Override
    public Boolean insertAlbum(AddAlbumReq req) {
        log.info("新增相册：{}",req.getAlbumName());
        BlogAlbum blogAlbum=blogAlbumMapper.selectByAlbumName(req.getAlbumName());
        if (blogAlbum!=null){
            throw new GlobalException(CodeMsg.ALBUM_EXISTED);
        }
        BlogAlbum insert=new BlogAlbum();
        insert.setAlbumId(UuidUtils.getId());
        insert.setAlbumName(req.getAlbumName());
        insert.setAlbumDescription(req.getAlbumDescription());
        insert.setAlbumPhotos(req.getAlbumPhotos());
        insert.setOrder(req.getOrder());
        blogAlbumMapper.insertSelective(insert);
        return true;
    }

    @Override
    public PageResult<BlogAlbumResp> selectPageAlbum(PageAlbumReq req) {
        log.info("查询相册列表：{}",req.getAlbumName());
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
       List<BlogAlbumResp> list= blogAlbumMapper.selectPageAlbum(req);
        PageInfo<BlogAlbumResp> info=new PageInfo<BlogAlbumResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }

    @Override
    public BlogAlbumResp selectAlbumDetail(String albumId) {
        log.info("查看相册详情：{}",albumId);
        BlogAlbum blogAlbum = blogAlbumMapper.selectByPrimaryKey(albumId);
        if (blogAlbum==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        //查看图册
        BlogAlbumResp resp = new BlogAlbumResp();
        BeanUtils.copyProperties(blogAlbum,resp);
        if (StringUtils.isNotBlank(resp.getAlbumPhotos())){
            List<PictureUrlResp> respList = imageMapper.selectImageList(resp.getAlbumPhotos());
            resp.setImageList(respList);
        }
        return resp;
    }

    @Override
    public Boolean updateAlbumDetail(AddAlbumReq req) {
        BlogAlbum blogAlbum = blogAlbumMapper.selectByPrimaryKey(req.getAlbumId());
        if (blogAlbum==null){
            throw new GlobalException(CodeMsg.ALBUM_ERROR);
        }
        BeanUtils.copyProperties(req,blogAlbum);
        blogAlbumMapper.updateByPrimaryKeyWithBLOBs(blogAlbum);
        return true;
    }

    @Override
    public Boolean deleteAlbum(String albumId) {
        blogAlbumMapper.updateStatus(albumId);
        return true;
    }

    @Override
    public PageResult<BlogAlbumResp> getAlbumList(PageAlbumReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<BlogAlbumResp> list= blogAlbumMapper.selectPageAlbum1(req);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlogAlbumResp resp : list) {
                PictureUrlResp pictureUrlResp = imageMapper.selectImage(resp.getAlbumPhotos());
                List<PictureUrlResp> arrayList = new ArrayList<>();
                arrayList.add(pictureUrlResp);
                resp.setImageList(arrayList);
            }
        }
        PageInfo<BlogAlbumResp> info=new PageInfo<BlogAlbumResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }
}
